package com.projectfixit.dao;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.projectfixit.entity.ContentDO;
import com.projectfixit.entity.ContentError;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.helper.DAOBase;

public class FixitDao extends DAOBase {
	static{
		ObjectifyService.register(ContentDO.class);
	}
	
	public int putContent(String content, String title, String author, String id, String errorContent){
		int result = -1;
		Objectify objectify = ObjectifyService.begin();
		
		ContentDO oldContent = null;
		try {
			oldContent = objectify.get(ContentDO.class, id);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		if(oldContent != null ){

			List<String> errorList = oldContent.getErrorContentList();
			
			if(errorList!= null && errorList.size()>0){
				errorList.add(errorContent);

			} else {
				errorList = new ArrayList<String>();
				errorList.add(errorContent);
			}
			objectify.put(oldContent);
			result = 0;

		}else {
			List<String> errorList = new ArrayList<String>();
			errorList.add(errorContent);
			ContentDO newContentDO = new ContentDO(content, title, author, id, errorContent, errorList);
			objectify.put(newContentDO);
			result = 0;
		}
		

		return result;
	}

}
